<?php

namespace App\Admin\Controllers;

use App\Models\AreaModel;
use App\Models\BasesModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Media\MediaManager;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class BasesController extends AdminController
{
    use HasResourceActions;

    protected $title = '基地';

    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.base'))
            ->description(trans('admin.list'))
            ->body($this->grid()->render());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BasesModel());

        $grid->base_id('基地ID')->sortable();
        //        $grid->project()->name('应用');
        $grid->name('基地名称');
        $grid->address('基地地址');
        $grid->area('基地面积');
        //        $states = [
        //            'on'  => ['value' => 1, 'text' => '正常'],
        //            'off' => ['value' => 0, 'text' => '禁用'],
        //        ];
        //        $grid->status('状态')->switch($states);

        $grid->created_at('添加时间')->sortable();

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->where(function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                }, '关键词')->placeholder('基地名称');
            });

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status')->radio([
                                                    1 => '正常',
                                                    0 => '拉黑',
                                                ]);
            });
        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BasesModel());
        $form->display('base_id', 'ID');
        $form->text('name', '基地名称')->required();
        $form->hidden('address_text', '');
        $form->distpicker([
                              'province_id' => '省',
                              'city_id'     => '市',
                              'district_id' => '区'
                          ], '请选择区域')
            ->attribute('data-value-type', 'code')
            ->default([
                          'province' => 130000,
                          'city'     => 130200,
                          'district' => 130203,
                      ])
            ->required();
        $form->text('address', '详细地址')->required();
        $form->latlong('latitude', 'longitude', '经纬度')->height(500)->default(['lat' => 22.54315951091646, 'lng' => 114.05935049057007]);;
        $form->number('area', '基地面积(亩)')->min(1)->required()->rules('required|regex:/^\d+$/', [
            'regex' => '联系方式必须为数字',
        ]);
        $form->text('master_name', '基地负责人')->required();
        $form->mobile('phone', '联系方式')->required();
        $states = [
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '停用', 'color' => 'danger'],
        ];
        $form->switch('status', '基地状态')->states($states);

        // 多图
        $form->multipleImage('pictures', '基地图片')->removable();

        //                        $form->mediaManagerSelect('images', '基地图片');
        $form->saving(function (Form $form) {
            if (!$form->province_id || !$form->city_id || !$form->district_id) {
                $error = new MessageBag([
                                            'title'   => '错误',
                                            'message' => '必须选择基地区域',
                                        ]);
                return back()->with(compact('error'))->withInput();
            }
            $areas              = AreaModel::whereIn('code', [$form->province_id, $form->city_id, $form->district_id])->pluck('name', 'code');
            $form->address_text = $areas[$form->province_id] . $areas[$form->city_id] . $areas[$form->district_id];
        });

        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');
        return $form;
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.base'))
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.base'))
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail($id)
    {
        $base = BasesModel::findOrFail($id);
        $show    = new Show($base);
        $show->panel()
            ->style('primary')
            ->title('基地详情');
        $show->base_id('基地ID');
        $show->name('基地名');
        $show->address_text('基地地址');
        $show->address('基地详细地址');
        $show->area('基地面积')->as(function ($area) {
            return "{$area}亩";
        });
        $show->longitude('经度');
        $show->latitude('纬度');
        $show->master_name('负责人');
        $show->phone('负责人联系方式');
        $show->pictures('基地图片')->image();
        $show->status('状态')->using(['1' => '正常', '0' => '禁用']);
        $show->created_at(trans('admin.created_at'));

        $show->updated_at(trans('admin.updated_at'));
        return $show;
    }
}
